<template>
  <div id="container"></div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
export default {
  data() {
    return {
      map: null,
      marker: null,
    };
  },
  mounted() {
    //DOM初始化完成进行地图初始化
    this.initMap();
  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: "a503f62db84dba58af028cf80401f4ca", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: [
          "AMap.AutoComplete",
          "AMap.PlaceSearch",
          "AMap.Scale",
          "AMap.OverView",
          "AMap.ToolBar",
          "AMap.MapType",
          "AMap.PolyEditor",
          "AMap.CircleEditor",
          "AMap.Ceocoder",
        ], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.map = new AMap.Map("container", {
            //设置地图容器id
            viewMode: "3D", //是否为3D地图模式
            zoom: 10, //初始化地图级别
            center: [116.397428, 39.90923], //设置地图中心点坐标, //初始化地图中心点位置
            layers: [new AMap.TileLayer.Satellite()], //设置图层,可设置成包含一个或多个图层的数组
            mapStyle: "amap://styles/whitesmoke", //设置地图的显示样式
            features: ["bg", "point", "road", "building"], // 设置地图上显示的元素种类, 支持'bg'（地图背景）、'point'（POI点）、'road'（道路）、'building'（建筑物）
            dragEnable: true, // 地图是否可通过鼠标拖拽平移, 默认为 true。此属性可被 setStatus/getStatus 方法控制
            zoomEnable: true, // 地图是否可缩放，默认值为 true。此属性可被 setStatus/getStatus 方法控制
            jogEnable: true, // 地图是否使用缓动效果，默认值为true。此属性可被setStatus/getStatus 方法控制
          });
          this.marker = new AMap.Marker({
            position: [116.468599, 39.995847],
            opacity: 1,
            zIndex: 2,
            icon: "https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
            anchor: "bottom-center", // 设置点标记锚点，可选值：'top-left','top-center','top-right', 'middle-left', 'center', 'middle-right', 'bottom-left', 'bottom-center', 'bottom-right'
            title: "我也不知道是哪里！", // 鼠标滑过点标记时的文字提示。不设置则鼠标滑过点标无文字提示
            clickable: true, // 点标记是否可点击，默认值: true
            text: {
              content: "香猪坊",
              direction: "right",
              style: {
                fontSize: 15,
                fillColor: "#fff",
                strokeColor: "rgba(255,0,0,0.5)",
                strokeWidth: 2,
                padding: [3, 10],
                backgroundColor: "yellow",
                borderColor: "#ccc",
                borderWidth: 3,
              },
            },
          });
          //鼠标点击marker弹出自定义的信息窗体
          this.marker.on("click", function () {
            infoWindow.open(this.map, this.marker.getPosition());
          });
          this.map.add(this.marker);
          //实例化信息窗体
          var title =
              '方恒假日酒店<span style="font-size:11px;color:#F00;">价格:318</span>',
            content = [];
          content.push(
            "<img src='http://tpc.googlesyndication.com/simgad/5843493769827749134'>地址：北京市朝阳区阜通东大街6号院3号楼东北8.3公里"
          );
          content.push("电话：010-64733333");
          content.push(
            "<a href='https://ditu.amap.com/detail/B000A8URXB?citycode=110105'>详细信息</a>"
          );
          var infoWindow = new AMap.InfoWindow({
            isCustom: true, //使用自定义窗体
            content: createInfoWindow(title, content.join("<br/>")),
            offset: new AMap.Pixel(16, -45),
          });
          // 在地图上打开信息窗体
          infoWindow.open(this.map, [116.397389, 39.909466]);
          //构建自定义信息窗体
          function createInfoWindow(title, content) {
            var info = document.createElement("div");
            info.className = "custom-info input-card content-window-card";

            //可以通过下面的方式修改自定义窗体的宽高
            info.style.width = "400px";
            // 定义顶部标题
            var top = document.createElement("div");
            var titleD = document.createElement("div");
            var closeX = document.createElement("img");
            top.className = "info-top";
            titleD.innerHTML = title;
            closeX.src = "https://webapi.amap.com/images/close2.gif";
            closeX.onclick = closeInfoWindow;

            top.appendChild(titleD);
            top.appendChild(closeX);
            info.appendChild(top);

            // 定义中部内容
            var middle = document.createElement("div");
            middle.className = "info-middle";
            middle.style.backgroundColor = "white";
            middle.innerHTML = content;
            info.appendChild(middle);

            // 定义底部内容
            var bottom = document.createElement("div");
            bottom.className = "info-bottom";
            bottom.style.position = "relative";
            bottom.style.top = "0px";
            bottom.style.margin = "0 auto";
            var sharp = document.createElement("img");
            sharp.src = "https://webapi.amap.com/images/sharp.png";
            bottom.appendChild(sharp);
            info.appendChild(bottom);
            return info;
          }

          //关闭信息窗体
          function closeInfoWindow() {
            this.map.clearInfoWindow();
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
};
</script>
<style scoped>
#container {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 800px;
}
</style>
